package middle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class huace2 {
    public static void main(String[] args) {
        String[] keys = new String[]{"IMSI", "CellID", "UserIP", "CID", "UID"};
        String log = "Apn:cnet,Qos:121212121212,CellID:4600175319,Imsi:460019852146201,GWIP:1.1.1.1,UserIp:2.2.2.2,CID:854710-336985-852,UID:1-2-3-4,pwd:huawei7410";
        System.out.println(logAnonymize(keys, log));
    }

    // 屏蔽日志关键信息
    private static String logAnonymize(String[] keys, String log) {
        // 在此补充你的代码
        // 敏感信息关键字转小写
        List<String> words = new ArrayList<>();
        for (String key : keys) {
            words.add(key.toLowerCase());
        }
        String[] logEntries = log.split(",");
        StringBuilder maskedLog = new StringBuilder();
        for (String entry : logEntries) {
            String[] keyValue = entry.split(":");
            String key = keyValue[0].trim();
            String value = keyValue[1].trim();
            if (key.equalsIgnoreCase("password") || key.equalsIgnoreCase("pwd")) {  // 处理psw的情况
                value = "******";
            } else if (words.contains(key.toLowerCase())) {// 处理在words中的情况
                if (key.toLowerCase().endsWith("ip")) {  // 在words中且以ip结尾
                    String[] ipSegments = value.split("\\.");
                    if (ipSegments.length == 4) { // 将ip的中间两端替换为三个*
                        ipSegments[1] = "***";
                        ipSegments[2] = "***";
                        value = String.join(".", ipSegments); // 再连接起来
                    }
                } else {
                    int length = value.length();
                    int startIndex = -1;
                    int count = 0;
                    for (int i = length - 1; i >= 0; i--) {
                        if (Character.isDigit(value.charAt(i))) {
                            if (startIndex == -1) {
                                startIndex = i;
                            }
                            count++;
                        } else {
                            if (count >= 4) {
                                break;
                            } else {
                                startIndex = -1;
                                count = 0;
                            }
                        }
                    }
                    if (startIndex != -1 && count >= 4) {

                        int hideEndIndex = startIndex - count / 4;

                        int hideStartIndex = hideEndIndex - count / 2 + 1;
                        StringBuilder stringBuilder = new StringBuilder(value);
                        for (int i = hideStartIndex; i <= hideEndIndex; i++) {
                            stringBuilder.setCharAt(i, '*');
                        }
                        value = stringBuilder.toString();
                    }
                }
            }
            maskedLog.append(key).append(":").append(value).append(",");
        }
        String maskedLogString = maskedLog.toString();
        if (maskedLogString.endsWith(",")) {
            maskedLogString = maskedLogString.substring(0, maskedLogString.length() - 1);
        }
        return maskedLogString;
    }
}
